package com.taskcollection.service;

import com.taskcollection.bean.ProjectTeamBean;
import com.taskcollection.mapper.ProjectTeam;
import com.taskcollection.util.DBConnection;
import com.taskcollection.util.DBUtil;
import com.taskcollection.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by haocongshun on 2016/7/26.
 */
public class ProjectTeamService {
    public ProjectTeamBean getById(String id){
        SqlSession sqlSession = MybatisUtil.getinstance().getFactory().openSession();
        try{
            ProjectTeam pt = sqlSession.getMapper(ProjectTeam.class);
            ProjectTeamBean ptb = pt.getById(id);
            StudentService ss = new StudentService();
            ptb.setProjectTeamLeader(ss.getById(ptb.getProjectTeam_leader()));
            ptb.setProjectDeputyTeamLeader(ss.getById(ptb.getProjectTeam_deputyLeader()));
            return ptb;
        }finally {
            sqlSession.close();
        }
    }
    public boolean add(){
        return true;
    }
    public List<ProjectTeamBean> getAll(String Teacher_id){
        List<ProjectTeamBean> projectTeamList = new ArrayList<ProjectTeamBean>();
        String sql = "SELECT * FROM projectteam a,(SELECT ProjectTeam_id,Teacher_id FROM projectinfo WHERE projectTeam_id!=0) b WHERE a.ProjectTeam_id=b.ProjectTeam_id AND b.Teacher_id=?";
        Connection conn = DBConnection.getinstance().getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            ps = conn.prepareStatement(sql);
            ps.setString(1,Teacher_id);
            rs = ps.executeQuery();
            while (rs.next()){
                ProjectTeamBean ptb = new ProjectTeamBean();
                ptb.setProjectTeam_id(rs.getString("ProjectTeam_id"));
                ptb.setProjectTeam_name(rs.getString("projectTeam_name"));
                ptb.setProjectTeam_count(rs.getString("projectTeam_count"));
                ptb.setProjectTeam_leader(rs.getString("projectTeam_leader"));
                ptb.setProjectTeam_deputyLeader(rs.getString("projectTeam_deputyLeader"));
                StudentService ss = new StudentService();
                ptb.setProjectTeamLeader(ss.getById(ptb.getProjectTeam_leader()));
                ptb.setProjectDeputyTeamLeader(ss.getById(ptb.getProjectTeam_deputyLeader()));
                projectTeamList.add(ptb);
            }
            return projectTeamList;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeResultSet(rs);
            DBUtil.closePreparedStatement(ps);
            DBUtil.closeConnection(conn);
        }
        return null;
    }
    public boolean change(ProjectTeamBean ptb){
        SqlSession sqlSession = MybatisUtil.getinstance().getFactory().openSession();
        try{
            ProjectTeam pt = sqlSession.getMapper(ProjectTeam.class);
            boolean flag = pt.change(ptb);
            sqlSession.commit();
            return flag;
        }finally {
            sqlSession.close();
        }
    }

}
